#include <allegro.h>
#include "richard.h"
#include <stdio.h>
#define nb 2



void draw_b(BITMAP *Background,BITMAP*Buffer)
{
	blit(Background, Buffer, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
}

void destroy_background(BITMAP * Background)
{
	destroy_bitmap(Background);
}

/*void load_richard (richard *r)
{
int i;
char tmp[50];
	for (i=0; i<nb; i++)
	{
	sprintf(tmp,"../graphs/right%d.bmp",i);
	load_bitmap(tmp, NULL);
	sprintf(tmp,"../graphs/left%d.bmp",i);
	load_bitmap(tmp, NULL);
	}
	r->arret=load_bitmap("../graphs/arret.bmp", NULL);
}
*/
void load_richard (richard *r)
{


	r->img_r0=load_bitmap("../graphs/right0.bmp", NULL);
	r->img_r1=load_bitmap("../graphs/right1.bmp", NULL);
	r->img_l0=load_bitmap("../graphs/left0.bmp", NULL);
	r->img_l1=load_bitmap("../graphs/left1.bmp", NULL);
	r->arret=load_bitmap("../graphs/arret.bmp", NULL);
}

void init_richard(richard *r)
{
r->x=50;
r->y=300;
r->dir=0;
}

void destroy_richard(richard *r)
{
	destroy_bitmap(r->img_r0);
	destroy_bitmap(r->img_l0);
	destroy_bitmap(r->img_r1);
	destroy_bitmap(r->img_l1);
	destroy_bitmap(r->arret);
}

void depl_richard_right(richard *r, BITMAP * Background)
{

	if (key[KEY_D] && (r->x < SCREEN_W -(r->arret->w))) 
	{
		(r->x)+=1;
		r->dir = 0; //droite
	}

}

void depl_richard_left(richard *r, BITMAP * Background)
{

	if (key[KEY_Q] && (r->x > 0)) 
	{
		(r->x)-=1;
		r->dir = 1; //gauche
	}

}

void jump(richard *r, BITMAP * Background)
{
	if (key[KEY_SPACE] && (r->y > 300) ) 
	{
	(r->y)-=5;
	r->dir = 2; //saut
	}

}

void arret(richard *r, BITMAP * Background)
{	
	(r->y)+=4;
	r->dir = 2; //Position d'arret
	usleep(5000);
}

void deplacement(richard *r, BITMAP * Background)
{	
	depl_richard_right(r, Background);
	depl_richard_left(r, Background);
	jump(r, Background);
	
	if ((r->y)<=SCREEN_H-148)
	{
	arret(r,Background);
	}	
}





void draw(richard r, BITMAP *Buffer)
{
	static int last_direction;
	int i=0;

	if (r.dir == 0)	//deplacement a droite
	{ 
		draw_sprite(Buffer, r.img_r0,r.x, r.y);
		last_direction=0;
		
		usleep(500);
		draw_sprite(Buffer, r.img_r1, r.x,r.y);
		//usleep(50000);
	}

	if(r.dir == 1) 	//deplacement a gauche
	{

		draw_sprite(Buffer, r.img_l0,r.x, r.y);
		last_direction=1;
		//usleep(50000);
		
	}
	if (r.dir == 2 && last_direction==0) {	//jumping right

		draw_sprite(Buffer, r.arret,r.x, r.y);
	}
	if (r.dir == 2 && last_direction==1) {	//jumping left

		draw_sprite(Buffer, r.arret,r.x, r.y);
	}
}

/*void fight(richard *r, pirate *p, BITMAP *Background)
{
int i=0;
if (i==4) {i=0;}
	if (key[KEY_ENTER] && r->x < (p->x)-3)
	{
	i++;
	draw_sprite(Buffer, r.right_player[1],r.x, r.y);
	destro_bitmap(p->img);
	}
}
*/
